
** South Africa
** Clean Wave 1

*************
*************

	** ROSTER
		use "$dir/rawdata/NIDS South Africa/wave1/HouseholdRoster_W1_Anon_V7.0.0.dta", clear

	** In household
		gen w1_inhh=w1_r_res==1 if !missing(w1_r_res)
					
	** Household size
		gen temp=1 if w1_inhh==1
		bys w1_hhid: egen w1_hh_size=total(temp)
		drop temp
		
	** Marital status
		gen w1_married=w1_r_marstt==1 if !missing(w1_r_marstt)

	** Save
		keep w1_hhid pid w1_inhh w1_hh_size w1_married
		tempfile roster
		save `roster', replace
		
*************
*************

	** INDIVIDUAL FILES (DERIVED)
		use "$dir/rawdata/NIDS South Africa/wave1/indderived_W1_Anon_V7.0.0.dta", clear
		
	** Female
		gen w1_female=w1_best_gen==2 if !missing(w1_best_gen) & w1_best_gen>0
	
	** Age
		gen w1_age=w1_best_age_yrs
		
	** Works
		gen w1_work_any_c=w1_empl_stat==3 if !missing(w1_empl_stat)
		
		keep w1_hhid pid w1_age w1_work_any_c w1_female
		tempfile indderiv
		save `indderiv', replace
			 
	** INDIVIDUAL FILES
		use "$dir/rawdata/NIDS South Africa/wave1/Adult_W1_Anon_V7.0.0.dta", clear
		
	** Employment
		gen w1_work_primaryocc=w1_a_em1==1 if !missing(w1_a_em1) & w1_a_em1>0
		gen w1_work_secondaryocc=w1_a_em2==1 if !missing(w1_a_em2) & w1_a_em2>0
		gen w1_work_selfemp=w1_a_ems==1 if !missing(w1_a_ems) & w1_a_ems>0
		gen w1_work_casual=w1_a_emc==1 if !missing(w1_a_emc) & w1_a_emc>0
		gen w1_work_agri=w1_a_emp==1 if !missing(w1_a_emp) & w1_a_emp>0
		gen w1_work_bizhelp=w1_a_emh==1 if !missing(w1_a_emh) & w1_a_emh>0
		
		egen w1_work_any=rowmax(w1_work_primaryocc w1_work_secondaryocc w1_work_selfemp w1_work_casual w1_work_agri w1_work_bizhelp)

	** Work hours
		foreach var in w1_a_em1hrs w1_a_em2hrs w1_a_emshrs w1_a_emchrs w1_a_emphrs w1_a_emhhrs {
			replace `var'=. if `var'<0
		}
		egen w1_work_hrs=rowtotal(w1_a_em1hrs w1_a_em2hrs w1_a_emshrs w1_a_emchrs w1_a_emphrs w1_a_emhhrs)
	
	** CESD
		foreach var in w1_a_emobth w1_a_emogo w1_a_emomnd w1_a_emodep w1_a_emoeff w1_a_emohope w1_a_emofear w1_a_emoslp w1_a_emohap w1_a_emolone {
			recode `var' -8=. -3=. -9=.
			recode `var' 1=0 2=1 3=2 4=3
		}
		replace w1_a_emohope=3-w1_a_emohope
		replace w1_a_emohap=3-w1_a_emohap
		egen w1_cesd_total=rowtotal(w1_a_emobth w1_a_emogo w1_a_emomnd w1_a_emodep w1_a_emoeff w1_a_emohope w1_a_emofear w1_a_emoslp w1_a_emohap w1_a_emolone)
		egen w1_cesd_count=rownonmiss(w1_a_emobth w1_a_emogo w1_a_emomnd w1_a_emodep w1_a_emoeff w1_a_emohope w1_a_emofear w1_a_emoslp w1_a_emohap w1_a_emolone)
		replace w1_cesd_total=. if w1_cesd_count<8 // don't score if more than 2 are missing
		
		gen w1_depressed=w1_cesd_total>=10
		
		keep w1_hhid pid w1_work_any w1_work_hrs w1_work_primaryocc w1_work_secondaryocc w1_work_selfemp w1_work_casual w1_work_agri w1_work_bizhelp w1_cesd_count w1_depressed
		tempfile indiv
		save `indiv', replace
		
*************
*************

	** HOUSEHOLD DATA 

		use "$dir/rawdata/NIDS South Africa/wave1/hhderived_W1_Anon_V7.0.0.dta", clear
		
		gen w1_urban=w1_geo2011==2 if !missing(w1_geo2011)
			// 1: traditional=rural, 2: urban=urban, 3: farms=rural
		
		gen w1_hh_size_check=w1_hhsizer
		keep w1_hhid w1_hh_size_check w1_urban
			
		tempfile hh
		save `hh', replace
		
*************
*************

	** APPEND
		
		use `hh', clear
		merge 1:m w1_hhid using `roster', assert(matched) nogen
		merge 1:1 w1_hhid pid using `indderiv', assert(master matched) gen(merge_indderiv)
		merge 1:1 w1_hhid pid using `indiv', assert(master matched) gen(merge_indiv)
		
		order w1_hhid pid
		drop merge*
		
		compare w1_work_any_c w1_work_any
		drop w1_work_any_c
		assert w1_hh_size_check==w1_hh_size
		drop w1_hh_size_check
		
		save "$dir/data/saf_w1_individual.dta", replace
		
	** Missing variables: 
	** Urban status
	
	** Illness and injury

	** Impact of illness and injury
		